using System;
using System.Collections.Generic;
using System.IO;
using System.IO.IsolatedStorage;
using FRS.Devices;
using Newtonsoft.Json;

namespace FRS
{
    public class FileManager
    {
        private const string CECC_FILE = "cecc_devices.txt";
        private const string NETIO_230B_FILE = "netio_230b_devices.txt";
        private const string SWITCHBOX_FILE = "switchbox_devices.txt";
        private const string SETTINGS_FILE = "settings.txt";
        
        private static readonly string localAppData = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
        private static readonly string userFilePath = Path.Combine(localAppData, "FRS");

        public static List<Device> CECC_ReadDeviceListFile()
        {
            try
            {
                using (var reader = new JsonTextReader(new StreamReader(Path.Combine(userFilePath, CECC_FILE))))
                {
                    var serializer = new JsonSerializer();

                    return serializer.Deserialize<List<Device>>(reader);
                }
            }
            catch (FileNotFoundException)
            {
                return new List<Device>();
            }
            catch (DirectoryNotFoundException)
            {
                return new List<Device>();
            }
        }

        public static void CECC_WriteDeviceListFile(List<Device> deviceList)
        {
            /*Create FRS-Directory, if it doesn't exist*/
            if (!Directory.Exists(userFilePath))
                Directory.CreateDirectory(userFilePath);
            /* first we delete the old file, afterwards we create the new one */
            if (File.Exists(Path.Combine(userFilePath, CECC_FILE)))
                File.Delete(Path.Combine(userFilePath, CECC_FILE));

            using (var writer = new JsonTextWriter(new StreamWriter(Path.Combine(userFilePath, CECC_FILE))))
            {
                var serializer = new JsonSerializer();

                serializer.Serialize(writer, deviceList);
            }
        }

        public static List<Device> NETIO_230B_ReadDeviceListFile()
        {
            try
            {
                using (var reader = new JsonTextReader(new StreamReader(Path.Combine(userFilePath, NETIO_230B_FILE))))
                {
                    var serializer = new JsonSerializer();

                    return serializer.Deserialize<List<Device>>(reader);
                }
            }
            catch (FileNotFoundException)
            {
                return new List<Device>();
            }
            catch (DirectoryNotFoundException)
            {
                return new List<Device>();
            }
        }

        public static void NETIO_230B_WriteDeviceListFile(List<Device> deviceList)
        {
            /*Create FRS-Directory, if it doesn't exist*/
            if (!Directory.Exists(userFilePath))
                Directory.CreateDirectory(userFilePath);
            /* first we delete the old file, afterwards we create the new one */
            if (File.Exists(Path.Combine(userFilePath, NETIO_230B_FILE)))
                File.Delete(Path.Combine(userFilePath, NETIO_230B_FILE));

            using (var writer = new JsonTextWriter(new StreamWriter(Path.Combine(userFilePath, NETIO_230B_FILE))))
            {
                var serializer = new JsonSerializer();

                serializer.Serialize(writer, deviceList);
            }
        }

        public static List<Device> SwitchBox_ReadDeviceListFile()
        {
            try
            {
                using (var reader = new JsonTextReader(new StreamReader(Path.Combine(userFilePath, SWITCHBOX_FILE))))
                {
                    var serializer = new JsonSerializer();

                    return serializer.Deserialize<List<Device>>(reader);
                }
            }
            catch (FileNotFoundException)
            {
                return new List<Device>();
            }
            catch(DirectoryNotFoundException)
            {
                return new List<Device>();
            }
        }

        public static void SwitchBox_WriteDeviceListFile(List<Device> deviceList)
        {
            /*Create FRS-Directory, if it doesn't exist*/
            if (!Directory.Exists(userFilePath))
                Directory.CreateDirectory(userFilePath);
            /* first we delete the old file, afterwards we create the new one */
            if (File.Exists(Path.Combine(userFilePath, SWITCHBOX_FILE)))
                File.Delete(Path.Combine(userFilePath, SWITCHBOX_FILE));

            using (var writer = new JsonTextWriter(new StreamWriter(Path.Combine(userFilePath, SWITCHBOX_FILE))))
            {
                var serializer = new JsonSerializer();

                serializer.Serialize(writer, deviceList);
            }
        }

        public static PersistentSettings ReadSettingsListFile()
        {
            try
            {
                using (var reader = new JsonTextReader(new StreamReader(Path.Combine(userFilePath, SETTINGS_FILE))))
                {
                    var serializer = new JsonSerializer();

                    return serializer.Deserialize<PersistentSettings>(reader);
                }
            }
            catch (FileNotFoundException)
            {
                return new PersistentSettings();
            }
            catch (DirectoryNotFoundException)
            {
                return new PersistentSettings();
            }
        }

        public static void WriteSettingsListFile(PersistentSettings data)
        {
            if (File.Exists(Path.Combine(userFilePath, SETTINGS_FILE)))
                File.Delete(Path.Combine(userFilePath, SETTINGS_FILE));

            using (var writer = new JsonTextWriter(new StreamWriter(Path.Combine(userFilePath, SETTINGS_FILE))))
            {
                var serializer = new JsonSerializer();

                serializer.Serialize(writer, data);
            }
        }
    }
}
